package com.bzframework.project.picture.manager;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.bzframework.pro.io.configuration.CosFileProperties;
import com.bzframework.project.picture.model.vo.UploadPictureVO;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.ImageInfo;
import com.qcloud.cos.model.ciModel.persistence.PicOperations;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @Author bingzhong
 * @Date 2025/1/21 22:30
 * @Description
 */
@Component
@RequiredArgsConstructor
public class PictureFileManager {

    private final COSClient cosClient;

    private final CosFileProperties cosFileProperties;

    /**
     * 图片服务上传文件
     * @param filePart 文件
     * @param uploadPrefix 文件路径前缀
     * @return 图片信息
     */
    public Mono<UploadPictureVO> uploadFile(FilePart filePart, String uploadPrefix) {

        return DataBufferUtils.join(filePart.content())
                .map(dataBuffer -> {
                    String filename = filePart.filename();
                    String uploadFileName = String.format("%s_%s.%s",
                                                          RandomUtil.randomString(16),
                                                          System.currentTimeMillis(),
                                                          FileUtil.getSuffix(filename));

                    String uploadFilePath = String.format("/%s/%s", uploadPrefix, uploadFileName);
                    ObjectMetadata objectMetadata = new ObjectMetadata();
                    int contentLength = dataBuffer.readableByteCount();
                    objectMetadata.setContentLength(contentLength);

                    // 返回图片时添加图片的宽高等信息
                    PicOperations picOperations = new PicOperations();
                    picOperations.setIsPicInfo(cosFileProperties.getIsPicInfo());

                    PutObjectRequest putObjectRequest = new PutObjectRequest(cosFileProperties.getBucketName(), uploadFilePath, dataBuffer.asInputStream(), objectMetadata);
                    putObjectRequest.setPicOperations(picOperations);
                    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
                    ImageInfo imageInfo = putObjectResult.getCiUploadResult().getOriginalInfo().getImageInfo();

                    double scale = imageInfo.getHeight() != 0
                            ? (imageInfo.getWidth() * 1.0 / imageInfo.getHeight())
                            : 0D;

                    return UploadPictureVO.builder()
                                          .url(uploadFilePath)
                                          .name(FileUtil.mainName(filename))
                                          .size(contentLength)
                                          .width(imageInfo.getWidth())
                                          .height(imageInfo.getHeight())
                                          .scale(scale)
                                          .format(imageInfo.getFormat())
                                          .build();
                });

    }


}
